﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarolLib.Control
{
    public class ModuleHelper
    {
        public static Guid DefaultMdi;

        public static Dictionary<Guid, MdiUserControl> LoadALLModules()
        {
            List<MdiUserControl> mdis = ModulesConfiguration.GetConfig().Modules.Values.ToList();

            Dictionary<Guid, MdiUserControl> dic = new Dictionary<Guid, MdiUserControl>();

            foreach (MdiUserControl mdi in mdis)
            {
                var contract = (ModuleContractAttribute)Attribute.GetCustomAttribute(mdi.GetType(), typeof(ModuleContractAttribute));
                mdi.Caption = contract.ModuleName;
                mdi.Category = contract.Category;
                mdi.ModuleID = contract.ModuleID;

                dic.Add(contract.ModuleID, mdi);
            }

            return dic;
        }

        public static MenuStrip CreateModuleMenu(List<MdiUserControl> mdiList, Action<object, EventArgs> clickHandler)
        {
            MenuStrip mainMenu = new MenuStrip();

            var groups = mdiList.GroupBy(m => m.Category);

            foreach (var group in groups)
            {
                ToolStripMenuItem topMenu = new ToolStripMenuItem();
                topMenu.Text = group.Key;
                topMenu.Name = group.Key + "ToolStripMenuItem";

                foreach (var mdi in group)
                {
                    ToolStripMenuItem subMenu = new ToolStripMenuItem();
                    subMenu.Text = mdi.Caption;
                    subMenu.Name = mdi.Caption + "ToolStripMenuItem";
                    subMenu.Tag = mdi.ModuleID;
                    subMenu.Click += new EventHandler(clickHandler);
                    //Image img = Image.FromFile(@"..\..\Image\" + dv[i]["ImageName"].ToString());
                    //subMenu.Image = img;
                    topMenu.DropDownItems.Add(subMenu);
                }

                mainMenu.Items.Add(topMenu);
            }

            mainMenu.Dock = DockStyle.Top;

            return mainMenu;
        }
    }
}
